<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User_mdl extends MY_Model {

    public function __construct(){
        parent::__construct();
        $this->table = 'user';
    }

    //用户权限注释
    public $_group = array(
        '1' => '超级管理员',
        '2' => '普通管理员',
        '3' => '普通会员',
        '4' => '<span style="color:red">未激活会员</span>'
    );

    /**
     * 用户登陆处理
     * @return [type] [description]
     */
    public function login($post){

        $this->db->where('email',$post['email']);
        $this->db->where('password',md5($post['password']));

        $res = $this->get_one();
        //将数据加入session
        unset($res['password']);
        $this->session->set_userdata(['user'=>$res]);
        if(empty($res)){
            return false;
        }
        return true;
    }

    //用户验证类
    public function _validation(){
        $this->load->library('form_validation');

        $this->form_validation->set_rules('password', '密码', 'required|regex_match[/^\w{6,20}/]',[
            'required' => '请填写正确的%s',
            'regex_match' => '请输入由大写、小写、数字、下划线的%s，长度为6-20个字符'
            ]);
        $this->form_validation->set_rules('password_v', '确认密码', 'matches[password]',[
            'matches' => '%s和密码不一致'
            ]);

        $this->form_validation->set_error_delimiters('<span class="am-icon-close am-icon-sm">  ', '</span><br/>');
    }

    /**
     * 用户修改
     */
    public function edit($post){
        $uid = $post['uid'];
        unset($post['uid']);
        $this->db-> where('id',intval($uid));
        $post['update_time'] = $_SERVER['REQUEST_TIME'];
        $res = $this->update($post);
        return $res;
    }

    /**
     * 新增用户
     */
    public function add($post){
        unset($post['password_v']);
        $post['password'] = md5($post['password']);
        $post['name'] = 'HZ_'.$_SERVER['REQUEST_TIME'].rand(1,99);
        $post['create_time'] = $post['update_time'] = $_SERVER['REQUEST_TIME'];
        $res = $this->create($post);
		
		//通知管理员
		$this->load->model('news_mdl');
		$this->news_mdl->send_user($res,'尊敬的用户您好，欢迎您注册'.$_SESSION['sys']['title'].',祝您生活愉快，事业幸福！');
		$this->news_mdl->send_user('g','管理员您好，平台新增了一个用户');
		
        return $res;
    }

}

/* End of file User_mdl.php */
/* Location: ./application/models/User_mdl.php */